package com.educal.Controller;

import com.educal.Bean.Student;
import com.educal.Repository.StuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by miyang on 2018/1/11.
 */
@RestController
public class StuController {
    @Autowired
    private StuRepository stuRepository;

    @PutMapping(value = "/students/password")
    public Map<String,Object> updatePwd(@RequestParam("stuId") String stuId,
                                        @RequestParam("stuPassword") String stuPassword ){
        Map<String,Object> result = new HashMap<>();
        result.put("code",400);
        Student stu = new Student();
        List<Student> list = stuRepository.findAll();
        for(int i=0; i<list.size();i++) {
            String sId = list.get(i).getStuid();
            if (sId.equals(stuId)) {
                stu.setStuid(sId);
                stu.setStuname(list.get(i).getStuname());
                stu.setStupassword(stuPassword);
                stu.setStuclass(list.get(i).getStuclass());
                stu.setStucollege(list.get(i).getStucollege());
                stuRepository.save(stu);
                result.put("code",200);
                result.put("stu",stu);
                break;
            }
        }
        return result;
    }
    @PutMapping(value = "/download")
    public Map<String,Object> downloadFile(org.apache.catalina.servlet4preview.http.HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> result = new HashMap<>();
        String fileName = "1.txt";
        if (fileName != null) {

            String realPath = "D://test//";

            File file = new File(realPath, fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition",
                        "attachment;fileName=" +  fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;

                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    result.put("code",200);
                    /*System.out.println("success");*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
